package com.yeswater.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.yeswater.rabbitmq.utils.RabbitmqUtil;

import java.nio.charset.StandardCharsets;

/**
 * Author : yeswater
 */

public class ReceiveLogs1 {

    public static final String EXCHANGE_NAME = "log";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitmqUtil.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        String queue = channel.queueDeclare().getQueue();
        channel.queueBind(queue, EXCHANGE_NAME, "log1");

        System.out.println("等待接收消息,把接收的消息打印在屏幕上......");

        // 接收消息
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            System.out.println("控制台打印接收到的消息: "+ new String(delivery.getBody(),StandardCharsets.UTF_8));
        };

        boolean autoAck = true;
        channel.basicConsume(queue,autoAck,deliverCallback,consumerTag -> {});

    }

}
